#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@his-pc>
#
# Distributed under terms of the MIT license.

"""

"""


import bisect


class Interval(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end


# class Solution:
#     """
#     @param intervals: Sorted interval list.
#     @param newInterval: new interval.
#     @return: A new interval list.
#     """
#     def format_i(self, intervals):
#         if not intervals:
#             return intervals
#         return [tuple(i) for i in intervals]

#     def 

#     def insert(self, intervals, newInterval):
#         # write your code here
#         intervals = self.format_i(intervals)
#         print(newInterval.__dict__)
#         newInterval = tuple(newInterval)

#         if not intervals:
#             return [newInterval]

#         print(intervals)
#         print(newInterval)

#         idx = bisect.bisect(intervals, newInterval)     # 不真正插入, 尝试插入(返回索引)
#         bisect.insort(intervals, newInterval)
#         if idx == 0:
#             [0, 1]
#             intervals[idx:idx+2] = self._merge([idx, idx+1], intervals)
#         else:
#             # [idx-1, idx, idx+1]
#             intervals[idx-1:idx+2] = self._merge([idx-1, idx, idx+1], intervals)

#         return intervals

#     def _merge(self, idxs, l):
#         first = l[idxs[0]]
#         new_l = [first]
#         length = len(l)
#         for i in range(1, len(idxs)):
#             if length <= i:
#                 break
#             new_l = self._merge_to_list(new_l, l[idxs[i]])

#         return new_l

#     def _merge_to_list(self, l, ele):
#         # 将ele合并到l中
#         fst = tuple(l[-1])
#         snd = tuple(ele)

#         # print("===================  fst %s=====================" % str(fst))
#         # print("===================  snd %s=====================" % str(snd))
#         if fst[1] >= snd[0]:
#             # 交叉, 合并
#             pre = l[0:-1]

#             most_left = fst[0]
#             most_right = max(fst[1], snd[1])
#             merge = (most_left, most_right)
#             if pre:
#                 return pre + [merge]
#             else:
#                 return [merge]
#         else:
#             # 原样返回
#             return l + [ele]


def test():
    s = Solution()
    l = [(1, 3), (4, 6)]
    l = []
    v = s.insert(l, (3, 4.5))
    print(v)

    l = [(1,5 )]
    v = s.insert(l, (2, 3))
    print(v)

def main():
    test()
    return
    print("start main")
    s = Solution()
    v = s._merge_to_list([(1, 3)], [1, 4])
    print(v)

    # v = s._merge_to_list([(1, 3), (3.05, 3.06)], [3.1, 4])
    v = s._merge_to_list( [(1, 3), (4, 6)], (4.1, 5.5) )
    print(v)

if __name__ == "__main__":
    main()
